package com.hmdp.controller;


import cn.hutool.core.bean.BeanUtil;
import com.hmdp.constant.RedisConstants;
import com.hmdp.entity.User;
import com.hmdp.entity.dto.LoginFormDTO;
import com.hmdp.entity.result.Result;
import com.hmdp.entity.vo.UserVo;
import com.hmdp.entity.UserInfo;
import com.hmdp.service.IUserInfoService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户Controller
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @Resource
    private IUserInfoService userInfoService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 发送手机验证码
     */
    @PostMapping("code")
    public Result sendCode(@RequestParam("phone") String phone) {
        // 发送短信验证码并保存验证码
        return userService.sendCode(phone);
    }

    /**
     * 登录功能
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginForm) {
        return userService.login(loginForm);
    }

    /**
     * 登出功能
     */
    @PostMapping("/logout")
    public Result logout(HttpSession session, HttpServletRequest request) {
        // 销毁ThreadLocal中的用户信息
        BaseContext.remove();
        // 销毁session
        session.invalidate();
        // 获取请求头中的token
        String tokenKey = RedisConstants.LOGIN_USER_KEY_PREFIX + request.getHeader("authorization");
        // 判断redis中的tokenKey是否存在
        if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(tokenKey))) {
            // 使redis中的token失效
            stringRedisTemplate.delete(tokenKey);
        }
        return Result.ok();
    }

    /**
     * 校验登录状态
     */
    @GetMapping("/me")
    public Result me() {
        // 获取当前登录的用户并返回
        UserVo userVo = BaseContext.get();
        return Result.ok(userVo);
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long userId) {
        // 查询详情
        UserInfo info = userInfoService.getById(userId);
        if (info == null) {
            // 没有详情，应该是第一次查看详情
            return Result.ok();
        }
        info.setCreateTime(null);
        info.setUpdateTime(null);
        // 返回
        return Result.ok(info);
    }

    /**
     * 根据id查询用户
     * @param userId
     * @return
     */
    @GetMapping("/{id}")
    public Result queryUserById(@PathVariable("id") Long userId){
        // 查询详情
        User user = userService.getById(userId);
        if (user == null) {
            return Result.ok();
        }
        UserVo userVo = BeanUtil.copyProperties(user, UserVo.class);
        // 返回
        return Result.ok(userVo);
    }

    /**
     * 用户签到
     * @return
     */
    @PostMapping("/sign")
    public Result sign() {
        return userService.sign();
    }

    @GetMapping("/sign/count")
    public Result signCount() {
        return userService.signCount();
    }
}
